// Logger.hh
// 结构化日志系统，支持日志级别控制和上下文信息
// 使用LogConfig统一管理配置

#ifndef MEDIA_UTIL_LOGGER_HH
#define MEDIA_UTIL_LOGGER_HH

#include <stdio.h>
#include <string>
#include <time.h>
#include <sys/time.h>
#include <pthread.h>
#include "LogConfig.hh"

// 日志级别枚举
enum class LogLevel {
    DEBUG = 0,
    INFO = 1,
    WARNING = 2,
    ERROR = 3,
    NONE = 4  // 禁用所有日志
};

// 日志器类
class Logger {
public:
    // 获取单例实例
    static Logger& getInstance();
    
    // 设置日志级别
    void setLevel(LogLevel level);
    
    // 设置日志输出目标（默认stderr）
    void setOutput(FILE* output);
    
    // 日志输出函数
    void log(LogLevel level, const char* file, int line, const char* function, const char* format, ...);
    
    // 便捷日志宏
    #define LOG_DEBUG(fmt, ...) Logger::getInstance().log(LogLevel::DEBUG, __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__)
    #define LOG_INFO(fmt, ...) Logger::getInstance().log(LogLevel::INFO, __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__)
    #define LOG_WARNING(fmt, ...) Logger::getInstance().log(LogLevel::WARNING, __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__)
    #define LOG_ERROR(fmt, ...) Logger::getInstance().log(LogLevel::ERROR, __FILE__, __LINE__, __FUNCTION__, fmt, ##__VA_ARGS__)
    
private:
    Logger();
    ~Logger();
    Logger(const Logger&) = delete;
    Logger& operator=(const Logger&) = delete;
    
    LogLevel fLevel;
    FILE* fOutput;
    pthread_mutex_t fMutex;
    
    // 获取级别字符串
    const char* levelToString(LogLevel level) const;
    
    // 获取时间戳字符串
    void getTimestamp(char* buffer, size_t size) const;
    
    // 获取线程ID
    unsigned long getThreadId() const;
};

// ErrorHandler - 错误处理和日志记录（合并自ErrorHandler.hh）
enum class VideoErrorCode {
    SUCCESS = 0,
    PARAMETER_ERROR,
    BUFFER_OVERFLOW,
    FRAME_DROPPED,
    RTSP_ERROR,
};

// 错误编号定义
enum class ErrorNumber {
    ERR_NULL_POINTER = 1001,
    ERR_INVALID_FRAME_DATA = 1002,
    ERR_BUFFER_ACCESS = 1003,
    ERR_LIFECYCLE_MANAGEMENT = 1004,
    ERR_MEMORY_ACCESS = 1005,
    ERR_INVALID_SIZE = 1006,
    ERR_SOURCE_CREATION = 1007,
    ERR_STREAM_STATE = 1008,
    ERR_SCHEDULER_TASK = 1009,
    ERR_REFERENCE_COUNT = 1010,
};

class ErrorHandler {
public:
    static void reportError(VideoErrorCode code, const char* message);
    static void logInfo(const char* message);
    static void reportErrorWithNumber(VideoErrorCode code, ErrorNumber errNum, const char* message);
    static void logInfoWithNumber(ErrorNumber errNum, const char* message);
};

#endif // MEDIA_UTIL_LOGGER_HH

